// pages/mine/mine.js
const host = require('../../config.js').host;
const network = require('../../utils/network.js');
const app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    host: host,
    userInfo: {
      photo: '1'
    },
    userOrderCount: '',
  },
  gotomybalance() {
    wx.navigateTo({
      url: '/pages/mybalance/mybalance',
    })
  },
  gotocurrency() {
    wx.navigateTo({
      url: '/pages/currency/currency',
    })
  },
  gotomyorder() {
    wx.navigateTo({
      url: '/pages/myorder/myorder',
    })
  },
  gotomessage() {
    wx.navigateTo({
      url: '/pages/message/message',
    })
  },
  gotoquestion() {
    wx.navigateTo({
      url: '/pages/question/question',
    })
  },
  gotorefund() {
    wx.navigateTo({
      url: '/pages/refund/refund',
    })
  },
  gotofeedback() {
    wx.navigateTo({
      url: '/pages/feedback/feedback',
    })
  },
  gotobuycoupon() {
    wx.navigateTo({
      url: '/pages/buycoupon/buycoupon',
    })
  },
  gotobindphone() {
    wx.navigateTo({
      url: '/pages/bindphone/bindphone',
    })
  },
  gotocoupon() {
    wx.navigateTo({
      url: '/pages/coupon/coupon',
    })
  },
  gotoexchange() {
    wx.navigateTo({
      url: '/pages/exchange/exchange',
    })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {

  },

  getInfo() {
    let that = this;
    network.requestGet(host + '/xcx/customer/getUserInfo', {}, res => {
       
      if (res.data.code == 200) {
        that.setData({
          userInfo: res.data.data,
        })
      } else {
        wx.showToast({
          title: res.data.msg ? res.data.msg : '请求失败',
          icon: "none",
          duration: 1200
        })
      }
    })
  },

  getOrder() {
    let that = this;
    network.requestGet(host + '/xcx/customerPaymentRecord/count', {}, res => {
     
      if (res.data.code == 200) {
        that.setData({
          userOrderCount: res.data.data,
        })
      } else {
        wx.showToast({
          title: res.data.msg ? res.data.msg : '请求失败',
          icon: "none",
          duration: 1200
        })
      }
    })
  },
  // 授权登录用户信息
  getUserProfile(e) {
    let that = this;
    wx.getUserProfile({
      desc: '用于完善会员资料', // 声明获取用户个人信息后的用途，后续会展示在弹窗中，请谨慎填写
      success: function (res) {
        // console.log('====',res); 
        that.updatePhotoData(res.userInfo)
      },
      fail: function (res) {
        // console.log('-----',res); 
      },
    })

  },
  // 更新头像
  updatePhotoData(e) {

    let that = this;
    let userInfo = that.data.userInfo;
    let nickName = e.nickName;
    let avatarUrl = e.avatarUrl;
    console.log(nickName + '===' + avatarUrl)
    let filePath = '';
    wx.getImageInfo({
      src: avatarUrl,
      success: function (sres) {
        filePath = sres.path;
        network.uploadFileLoading("/common/upload", filePath, res => {
          let data = JSON.parse(res.data);
          avatarUrl = data.fileName;
          console.log("avatarUrl", avatarUrl)
          network.requestPost(host + '/xcx/customer/update-customer', {
            id: userInfo.id,
            nickName: nickName,
            photo: avatarUrl
          }, res => {
            if (res.data.code == '200') {
              wx.showToast({
                title: '登录成功',
                icon: 'success'
              })
              userInfo.nickName = nickName;
              userInfo.photo = avatarUrl;
              that.setData({
                userInfo: userInfo
              })
              console.log("userInfo=" + that.data.userInfo)
              app.globalData.userInfo = userInfo;
            } else {
              wx.showToast({
                title: res.data.msg,
                icon: 'none'
              })
            }
          })
        })
      }
    })



    // network.requestPost(host + '/xcx/customer/update-customer', {
    //   "id":that.data.userInfo.id,
    //   "photo":e
    // }, res => {
    //   console.log("editCustomer=", res)
    //   if (res.data.code == 200) {

    //     that.setData({
    //       'userInfo.photo': e
    //     })
    //     wx.showToast({
    //       title: '更新成功',
    //     })
    //   } else {
    //     wx.showToast({
    //       title: res.data.msg ? res.data.msg : '请求失败',
    //       icon: "none",
    //       duration: 1200
    //     })
    //   }
    // })
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    this.getInfo();
    this.getOrder();
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})